import { Directive, ViewContainerRef, TemplateRef, Input } from '@angular/core';

@Directive({
  selector: '[appMyForOf]',
})
export class MyForOfDirective {
  constructor(
    private viewContainer: ViewContainerRef,
    private templateRef: TemplateRef<any>
  ) {}

  @Input() appMyForOf: any[] = [];

  ngOnInit() {
    for (let i = 0; i < this.appMyForOf.length; i++) {
      this.viewContainer.createEmbeddedView(this.templateRef, {
        $implicit: this.appMyForOf[i],// 这里自行体会，为什么要用$implicit来抛出变量
        index: i,
      });
    }
  }
}
